import { asyncRouterMap, constantRouterMap } from "src/router";
import { getAllMenus } from "api/login";
/**
 * 通过authority判断是否与当前用户权限匹配
 * @param menus
 * @param route
 */
/* function hasPermission(menus, route) {
    if (route.authority) {
        if (menus[route.filterAsyncRouter] !== undefined) {
            return menus[route.authority]; //返回一个路由串？
        } else {
            return false; //route.filterAsyncRouter=undefined的时候返回false？
        }
    } else {
        return true; //route.authority==false？
    }
} */

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param asyncRouterMap
 * @param roles
 */
/* function filterAsyncRouter(asyncRouterMap, menus, menuDatas) {
    console.log(menuDatas, 9696);

    const accessedRouters = asyncRouterMap.filter((route) => {
        console.log(menuDatas, route, 888, menus, '每次是否有异步--', hasPermission(menus, route));

        if (hasPermission(menus, route)) {
            console.log(menuDatas, route, 888999888999);
            if (route.authority != "groupManager") {
                route.name = menuDatas[route.authority].title;
                route.icon = menuDatas[route.authority].icon;
                if (route.children && route.children.length) {
                    route.children = filterAsyncRouter(route.children, menus, menuDatas);
                }
                return true; //route有子元素返回true
            }
            // route.name = menuDatas[route.authority].title;
            // route.icon = menuDatas[route.authority].icon;
            // if (route.children && route.children.length) {
            //   route.children = filterAsyncRouter(route.children, menus, menuDatas);
            // }
            // return true //route有子元素返回true
        }
        return false; //route无子元素返回false
    });
    console.log('异步路由--', accessedRouters);
    return accessedRouters;
} */

const permission = {
    state: {
        routers: constantRouterMap,
        addRouters: [],
    },
    mutations: {
        // SET_ROUTERS: (state, routers) => {
        //     state.routers = constantRouterMap.concat(routers);
        //     state.addRouters = routers;
        // },
    },
    actions: {
        GenerateRoutes({ commit }, menus) {
            return new Promise((resolve) => {
                getAllMenus().then((data) => {
                    console.log(data, 357);
                    const menuDatas = {};
                    for (let i = 0; i < data.length; i++) {
                        menuDatas[data[i].code] = data[i];
                    }
                    /*  const accessedRouters = filterAsyncRouter(
                         asyncRouterMap,
                         menus,
                         menuDatas
                     ); */
                    // console.log(accessedRouters, 3589);
                    // commit("SET_ROUTERS", accessedRouters);
                    commit("SET_ROUTERS", []);
                    resolve();
                });
            });
        },
    },
};

export default permission;